package xyz.xtt.exchange.controller;

import java.math.BigDecimal;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tongtong.share.vo.CredentialRes;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.common.response.ApiResult;
import xyz.xtt.exchange.service.IInnerService;
import xyz.xtt.exchange.service.IUserCollectService;
import xyz.xtt.exchange.service.IUserLikeService;
import xyz.xtt.exchange.service.NftService;

/**
 * @Description: 数字藏品接口
 * @Date: 2024/11/20/
 * @Author: gzm
 */
@Slf4j
@RequestMapping("/api/exchange/nft")
@RestController
public class NftController {

	@Autowired
	private NftService nftService;
	@Autowired
	private IUserCollectService userCollectService;
	@Autowired
	private IUserLikeService userLikeService;

	@Autowired
	private IInnerService iInnerService;

//	/**
//	 * 分页获取我的nft列表
//	 *
//	 * @return ApiResult<List < NftListVo>>；
//	 */
//	@Operation(summary = "获取用户藏品列表", description = "uid，cateGory必须存在")
//	@GetMapping("/myNftList")
//	public ApiResult<List<NftListVo>> getNftListByUid(@RequestHeader("uid") String uid, @RequestParam(value = "currPage", defaultValue = "1") Integer currPage,
//			@RequestParam(value = "pageSize", defaultValue = "10") @Max(value = 100, message = "每页数量不能超过100") Integer pageSize,
//			@RequestParam(value = "queryType", required = true) String queryType, // queryType 0 拥有的 1 铸造的， 2 我的收藏列表，3 我生产的
//			@RequestParam(value = "businessType", required = true) int businessType, @RequestParam(value = "orderType", required = false) int orderType,
//			HttpServletRequest req) {
//		log.info("requestUri: /api/exchange/nft/myNftList，requestParams: uid:{},cateGory:{},currPage:{},pageSize:{}", uid, currPage, pageSize);
//		List<NftListVo> nftList = null;
////        if (Optional.ofNullable(uid).isEmpty()) {
////            return ApiResult.failure(1, "数据不存在");
////        }
//
//		// 我的收藏
//		if ("2".equals(queryType)) {
//			nftList = userCollectService.getCollectList(uid, currPage, pageSize, businessType, orderType);
//		} else {
//			nftList = nftService.getNftListByUidV2(uid, currPage, pageSize, queryType, businessType, orderType);
//		}
//
//		if (nftList == null) {
//			return ApiResult.failure(1, "数据不存在");
//		} else {
//			return ApiResult.ok(nftList);
//		}
//	}

//	@Operation(summary = "获取用户藏品列表", description = "uid必须存在")
//	@GetMapping("/getMyCollectNftList")
//	public ApiResult<List<NftListVo>> getMyCollectNftList(@RequestHeader("uid") String uid,
//			@RequestParam(value = "currPage", defaultValue = "1") Integer currPage,
//			@RequestParam(value = "pageSize", defaultValue = "10") @Max(value = 100, message = "每页数量不能超过100") Integer pageSize) {
//		log.info("requestUri: /api/exchange/nft/myNftList，requestParams: uid:{},cateGory:{},currPage:{},pageSize:{}", uid, currPage, pageSize);
//		List<NftListVo> nftList = null;
//		// 我的收藏
//		nftList = userCollectService.getCollectList(uid, currPage, pageSize, -1, 0);
//
//		if (nftList == null) {
//			return ApiResult.failure(1, "数据不存在");
//		} else {
//			return ApiResult.ok(nftList);
//		}
//	}

//	@Operation(summary = "获取用户铸造作品列表", description = "uid必须存在")
//	@GetMapping("/getMyBuildNftList")
//	public ApiResult<List<NftListVo>> getMyBuildNftList(@RequestHeader("uid") String uid,
//			@RequestParam(value = "currPage", defaultValue = "1") Integer currPage, @RequestParam(value = "signUserId") String signUserId,
//			@RequestParam(value = "pageSize", defaultValue = "10") @Max(value = 100, message = "每页数量不能超过100") Integer pageSize) {
//		log.info("requestUri: /api/exchange/nft/myNftList，requestParams: uid:{},cateGory:{},currPage:{},pageSize:{}", uid, currPage, pageSize);
//		List<NftListVo> nftList = null;
//		// 我的铸造
//		nftList = nftService.getNftListByUidV2(StringUtils.hasText(signUserId) ? signUserId : uid, currPage, pageSize, "1", -1, 0);
//		if (nftList == null) {
//			return ApiResult.failure(1, "数据不存在");
//		} else {
//			return ApiResult.ok(nftList);
//		}
//	}

//	@Operation(summary = "获取用户拥有作品列表", description = "uid必须存在")
//	@GetMapping("/getMyOwnNftList")
//	public ApiResult<List<NftListVo>> getMyOwnNftList(@RequestHeader("uid") String uid, @RequestParam(value = "currPage", defaultValue = "1") Integer currPage,
//			@RequestParam(value = "pageSize", defaultValue = "10") @Max(value = 100, message = "每页数量不能超过100") Integer pageSize) {
//		log.info("requestUri: /api/exchange/nft/myNftList，requestParams: uid:{},cateGory:{},currPage:{},pageSize:{}", uid, currPage, pageSize);
//		List<NftListVo> nftList = null;
//		// 我的收藏
//		nftList = nftService.getNftListByUidV2(uid, currPage, pageSize, "0", -1, 0);
//		if (nftList == null) {
//			return ApiResult.failure(1, "数据不存在");
//		} else {
//			return ApiResult.ok(nftList);
//		}
//	}

//	/**
//	 * 分页获取系列藏品
//	 *
//	 * @return ApiResult<List < NftListVo>>；
//	 */
//	@Operation(summary = "获取系列藏品列表", description = "系列id必须存在")
//	@GetMapping("/collectionNftList")
//	public ApiResult<List<NftListVo>> getCollectionNftList(@RequestParam(value = "collectionId") Long collectionId,
//			@RequestParam(value = "currPage", defaultValue = "0") Integer currPage, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
//			HttpServletRequest req) {
//		String uid = req.getHeader("uid");
//		log.info("requestUri: /api/exchange/nft/collectionNftList，requestParams: uid{},collectionId{}", uid, collectionId);
//		List<NftListVo> collectionList = null;
//		if (Optional.ofNullable(collectionId).isPresent()) {
//			collectionList = nftService.getCollectionNftListById(collectionId, currPage, pageSize);
//		}
//		if (collectionList == null) {
//			return ApiResult.failure(1, "数据不存在");
//		} else {
//			return ApiResult.ok(collectionList);
//		}
//	}

//	/**
//	 * 获取nft详情
//	 *
//	 * @return ApiResult<NftDetailsVo>；
//	 */
//	@Operation(summary = "获取藏品权益详情", description = "goodsId")
//	@GetMapping("/nftDetails")
//	public ApiResult<NftDetailsVo> getNftDetailsById(@RequestParam("goodsId") String goodsId, HttpServletRequest req) {
//		String uid = req.getHeader("uid");
//		log.info("requestUri: /api/exchange/nft/nftDetails，requestParams: uid{},goodsId{}", uid, goodsId);
//		NftDetailsVo nftDetailsVo = nftService.getNftDetailsById(goodsId);
//		return ApiResult.ok(nftDetailsVo);
//	}

	/**
	 * 获取oss临时token
	 *
	 * @return ApiResult<String>；
	 */
	@Operation(summary = "获取oss临时token", description = "uid必须存在")
	@GetMapping("/getTempToken")
	public ApiResult<CredentialRes> getTempToken(@RequestHeader("uid") String uid, HttpServletRequest req) {
		log.info("requestUri: /api/exchange/nft/getTempToken，requestParams uid:{}", uid);
		CredentialRes credential = iInnerService.getCredential();
		if (Objects.isNull(credential)) {
			return ApiResult.failure(1, "获取临时token失败");
		}
		return ApiResult.ok(credential);
	}

//	/**
//	 * @param type     类型
//	 * @param currPage 当前页从1开始
//	 * @return List<NftQueryResponse>
//	 */
//	@Operation(summary = "NFT查询")
//	@GetMapping("/list")
//	public ApiResult<List<NftListVo>> list(Integer currPage, @RequestParam(required = false) String type, @RequestParam(required = false) String searchKey) {
//		List<NftListVo> list = nftService.list(currPage, type, searchKey);
//		return ApiResult.ok(list);
//	}

	@Operation(summary = "铸造Token")
	@PostMapping("/mintToken")
	public ApiResult<String> mintToken(@RequestHeader("uid") String uid) {
		log.info("/api/exchange/nft/mint uid:{}", uid);
		String token = nftService.mintToken(uid);
		return ApiResult.ok(token);
	}

	@GetMapping("/is_like")
	public ApiResult<Boolean> getLike(@RequestHeader("uid") String uid, String goodsId) {
		Long userId = Long.parseLong(uid);
		return ApiResult.ok(userLikeService.isLike(userId, goodsId));
	}

	@GetMapping("/like")
	public ApiResult<Boolean> setLike(@RequestHeader("uid") String uid, String goodsId, int status) {
		Long userId = Long.parseLong(uid);
		return ApiResult.ok(userLikeService.like(userId, goodsId, status));
	}

//	@Operation(summary = "铸造NFT")
//	@PostMapping("/mint")
//	public ApiResult<MintResponse> mint(@RequestHeader("uid") String uid, @RequestBody @Valid MintRequestVo mintRequestVo) {
//		log.info("/api/exchange/nft/mint uid:{}, params:{}", uid, JSONUtil.toJsonStr(mintRequestVo));
//		// TODO 临时处理
//		mintRequestVo.setAmount(1);
//		MintResponse mint = nftService.mint(uid, mintRequestVo);
//		return ApiResult.ok(mint);
//	}

//	@Operation(summary = "铸造权")
//	@PostMapping("/reMint")
//	public ApiResult<MintResponse> reMint(@RequestHeader("uid") String uid, @RequestBody @Valid ReMintRequest reMintRequest) {
//		log.info("/api/exchange/nft/reMint uid:{}, params:{}", uid, JSONUtil.toJsonStr(reMintRequest));
//		// TODO 临时处理
//		MintResponse mint = nftService.reMint(uid, reMintRequest);
//		return ApiResult.ok(mint);
//	}

//	@Operation(summary = "资产中心铸造NFT")
//	@PostMapping("/domint")
//	public ApiResult<MintResponse> domint(@RequestHeader("uid") String uid, @RequestBody @Valid MintRequestVo mintRequestVo) {
//		log.info("/api/exchange/nft/mint uid:{}, params:{}", uid, JSONUtil.toJsonStr(mintRequestVo));
//		MintResponse mint = nftService.assetMint(uid, mintRequestVo);
//		return ApiResult.ok(mint);
//	}

//	@Operation(summary = "热卖榜")
//	@GetMapping("/get_hot_top")
//	public ApiResult<List<HotTopAssetVo>> getHotTopList() {
//		return ApiResult.ok(tblAssetTransactionService.getHotTopList());
//	}

//	@Operation(summary = "豪情榜")
//	@GetMapping("/get_top_con")
//	public ApiResult<TopConsumeResponse> getTopConsume(@RequestHeader("uid") Long uid) {
//		return ApiResult.ok(iPayOrderService.getHotConsume(uid));
//	}

	@Operation(summary = "最高价")
	@GetMapping("/buyMaxPriceWithHistory")
	public ApiResult<BigDecimal> buyMaxPriceWithHistory(@RequestHeader("uid") String uid, String goodsId) {
		BigDecimal bigDecimal = iInnerService.buyMaxPriceWithHistory(uid, goodsId);
		return ApiResult.ok(bigDecimal);
	}

}
